'''
@author: harumonia
@site: 
@datetime: 2020/3/15 17:15
@software: PyCharm
'''
import datetime
import os
import random

from application.models import Article
from application.utils.file_handler import gen_rnd_filename
from config.baseconfig import ConfigBase


def upload_getter(fileobj, file_type):
    fname, fext = os.path.splitext(fileobj.filename)
    rnd_name = '%s%s' % (gen_rnd_filename(), fext)
    filepath = os.path.join('application\static', file_type, rnd_name)
    callpath = os.path.join('\static', file_type, rnd_name)
    dirname = os.path.dirname(filepath)
    if not os.path.exists(dirname):
        try:
            os.makedirs(dirname)
        except:
            error = 'ERROR_CREATE_DIR'

    fileobj.save(filepath)

    res = {
        'fileName': rnd_name,
        'uploaded': 1,
        'url': "http://" + ConfigBase.SERVERHOST + callpath
    }

    return res


def add_article(data):
    art = Article()
    art.init(data)
